/**
 * @file BaseData.h
 * @author shikang999@126.com
 * @brief 
 * @version 0.1
 * @date 2025-06-18
 * 
 * @copyright Copyright (c) 2025
 * 
 */
#pragma once
#include <cmath>
#include <vector>
#include <memory>
#include <atomic>
#include <string>
#include <cstdint>
#include <iostream>
#include <eigen3/Eigen/Dense>

namespace Solver
{
//1.
using value_t    = double;
using VectorXd_t = Eigen::Matrix<value_t, -1,  1>;
using MatrixXd_t = Eigen::Matrix<value_t, -1, -1>;
using vecValue_t = std::vector<value_t>;
using minFun_t   = value_t (*)(const VectorXd_t &);
using normFun_t  = void(*)(VectorXd_t &);

constexpr value_t ConstEps1E12 = 1e-12;

//2.单个结果
struct DataValue_t
{
    DataValue_t(uint32_t dim):y(std::numeric_limits<value_t>::max())
    {
        x.resize(dim);
    }

    value_t    y;//目标值
    VectorXd_t x;//变量值
};
using vecDataValue_t = std::vector<std::shared_ptr<DataValue_t>>;

//3.个体变量
struct Variable_t
{
    Variable_t(uint32_t index, uint32_t dim):id(index),
                                             num(0),
                                             best(std::make_shared<DataValue_t>(dim)),
                                             current(std::make_shared<DataValue_t>(dim)),
                                             opt(std::make_shared<DataValue_t>(dim))
    {

    }

    uint32_t id;
    uint32_t num;                        //多少个迭代次没有更新最佳结果了
    std::shared_ptr<DataValue_t> best;   //历史最好结果
    std::shared_ptr<DataValue_t> current;//当前迭代次结果
    std::shared_ptr<DataValue_t> opt;    //优化时的结果
};
using vecVariable_t = std::vector<std::shared_ptr<Variable_t>>;

//4.迭代时的条件
struct Condition_t
{
    Condition_t():  id(0),
                    run(false),
                    stop(false), 
                    popNum(90),
                    limitIter(20000), 
                    currentInter(0), 
                    limitError(0), 
                    currentError(std::numeric_limits<value_t>::max())
    {

    }

    uint32_t id;          //顺序ID
    std::atomic_bool run; //是否在求解
    std::atomic_bool stop;//是否需要停止
    uint32_t popNum;      //种群数量
    uint32_t limitIter;   //最大迭代次数
    uint32_t currentInter;//当前迭代次数
    value_t  limitError;  //最小终止误差
    std::atomic<value_t> currentError;//当前最小误差
};

using vecCondition_t = std::vector<std::shared_ptr<Condition_t>>;

}